package com.somlsis.activity

import android.view.LayoutInflater
import android.view.MenuItem
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.base.BaseActivity
import com.clj.fastble.data.BleDevice
import com.google.android.material.navigation.NavigationBarView
import com.somlsis.R
import com.somlsis.adapter.FragmentAdapter
import com.somlsis.databinding.ActivityHomeBinding
import com.somlsis.fragment.BasicInfoDeviceFragment
import com.somlsis.fragment.DeviceFragment
import com.somlsis.fragment.HomeFragment
import com.somlsis.fragment.MineFragment

class HomeActivity : BaseActivity<ActivityHomeBinding>() {
    var menuItem: MenuItem? = null
    private var fragmentList = ArrayList<Fragment>()
    private lateinit var homeFragment: HomeFragment
    private lateinit var deviceFragment: DeviceFragment
    private lateinit var mineFragment: MineFragment
    override fun getViewBinding(): ActivityHomeBinding {
        return ActivityHomeBinding.inflate(LayoutInflater.from(this))
    }

    override fun createView() {
        binding.homeBnv.itemIconTintList = null
        homeFragment= HomeFragment(object : OnHomeAddDeviceNextClickListener {
            override fun onAddDeviceClick() {
                super.onAddDeviceClick()
                binding.homeVp.currentItem = 1
            }
        })
         deviceFragment = DeviceFragment()
        mineFragment = MineFragment()

        fragmentList.add(homeFragment)
        fragmentList.add(deviceFragment)
        fragmentList.add(mineFragment)

        binding.homeVp.adapter = FragmentAdapter(this, fragmentList)
        binding.homeVp.registerOnPageChangeCallback(object :
            ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(page: Int) {
                menuItem = binding.homeBnv.menu.getItem(page)
                menuItem?.isChecked = true
            }
        })
        binding.homeBnv.setOnItemSelectedListener(object :
            NavigationBarView.OnItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                menuItem = item
                when (item.itemId) {
                    R.id.menu_home -> {
                        binding.homeVp.currentItem = 0
                        return true
                    }

                    R.id.menu_device -> {
                        binding.homeVp.currentItem = 1
                        return true
                    }

                    else -> {
                        binding.homeVp.currentItem = 2
                    }
                }
                return false

            }
        })
        binding.homeVp.isUserInputEnabled = false

    }

    interface OnHomeAddDeviceNextClickListener {
        fun onAddDeviceClick() {}
    }
}